package com.molin.extensionTest.getFieldModel;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class Demo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //1.获取Class对象
        Class clazz = Class.forName("com.molin.extensionTest.getFieldModel.Student");

        //2.获取name这个Filed对象
        Field name = clazz.getField("name");

        //3.利用set方法进行赋值
        //3.1先创建一个Student对象
        Constructor constructor = clazz.getConstructor();
        Student zs = (Student) constructor.newInstance();
        //3.2有了对象才可以给指定对象进行赋值
        name.set(zs,"张三");

        System.out.println(name.get(zs));
    }
}
